﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Dapper;
using PMS.IRepository;
using PMS.Models.Base;
using PMS.Utils;

namespace PMS.Repository
{
    public class User_PowerRepository : BaseRepository<User_Power>, IUser_PowerRepository
    {
        #region 公共方法


        public int Add(User_Power user)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into User_Power(");
            strSql.Append("UserId,PowerId,CreateBy,CreateDate,ModifyBy,ModifyDate)");
            strSql.Append(" values (");
            strSql.Append("@UserId,@PowerId,@CreateBy,@CreateDate,@ModifyBy,@ModifyDate)");
            using (DbConn)
            {
                return DbConn.Execute(strSql.ToString(), user);
            }
        }

        public int Edit(User_Power user)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update User_Power set ");
            strSql.Append("CreateBy=@CreateBy,");
            strSql.Append("CreateDate=@CreateDate,");
            strSql.Append("ModifyBy=@ModifyBy,");
            strSql.Append("ModifyDate=@ModifyDate");
            strSql.Append(" where UserId=@UserId and PowerId=@PowerId ");
            using (DbConn)
            {
                return DbConn.Execute(strSql.ToString(), user);
            }
        }

        public User_Power Dtl(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,UserName,PassWord,RegisterDate,LastLoginDate,IsDel,UpdateBy,UpdateDate,DeleteBy,DeleteDate from User_Power ");
            strSql.Append(" where Id=@Id");
            using (DbConn)
            {
                return DbConn.QueryFirstOrDefault<User_Power>(strSql.ToString(), new { Id = id });
            }
        }

        public bool IsExists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from User_Power");
            strSql.Append(" where Id=@Id");
            using (DbConn)
            {
                return DbConn.ExecuteScalar(strSql.ToString(), new { Id = id }).ToInt32() > 0;
            }
        }


        public int Delete(int id, string deleteBy)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("Update User_Power  Set ");
            strSql.Append(" IsDel=1, ");
            strSql.Append(" DeleteBy=@DeleteBy, ");
            strSql.Append(" DeleteDate=GetDate() ");
            strSql.Append(" where Id=@Id");
            using (DbConn)
            {
                return DbConn.Execute(strSql.ToString(), new { Id = id, DeleteBy = deleteBy });
            }
        }

        public int Delete(int[] ids, string deleteBy)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("Update User_Power  Set ");
            strSql.Append(" IsDel=1, ");
            strSql.Append(" DeleteBy=@DeleteBy, ");
            strSql.Append(" DeleteDate=GetDate() ");
            strSql.Append(" where Id in @Ids");
            using (DbConn)
            {
                return DbConn.Execute(strSql.ToString(), new { Id = ids.ToArray(), DeleteBy = deleteBy });
            }
        }

        public List<User_Power> GetList()
        {
            string strSql = "Select * from User_Power";
            using (DbConn)
            {
                return DbConn.Query<User_Power>(strSql).ToList();
            }
        }

        public List<User_Power> GetList(Hashtable ht)
        {
            throw new NotImplementedException();
        }

        public int GetCount(Hashtable ht)
        {
            throw new NotImplementedException();
        }

        public void GetStrWhereAndParam(Hashtable ht, StringBuilder sb, Dictionary<string, object> param)
        {
            throw new NotImplementedException();
        }

        #endregion

    }
}